package com.zj.seckill.controller;


import com.zj.seckill.domain.MiaoshaOrder;
import com.zj.seckill.domain.MiaoshaUser;
import com.zj.seckill.domain.OrderInfo;
import com.zj.seckill.redis.GoodsKey;
import com.zj.seckill.redis.MiaoshaUserKey;
import com.zj.seckill.result.CodeMsg;
import com.zj.seckill.result.Result;
import com.zj.seckill.service.GoodsService;
import com.zj.seckill.service.MiaoshaService;
import com.zj.seckill.service.MiaoshaUserService;
import com.zj.seckill.service.OrderService;
import com.zj.seckill.util.RedisUtil;
import com.zj.seckill.vo.GoodsDetailVo;
import com.zj.seckill.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.context.webflux.SpringWebFluxContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	MiaoshaUserService userService;

	@Autowired
	RedisUtil redisUtil;

	@Autowired
	GoodsService goodsService;

	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;

	@Autowired
	ApplicationContext applicationContext;


	@RequestMapping(value = "/to_list",produces = "text/html")
	@ResponseBody
	public String toList(Model model, MiaoshaUser user, HttpServletRequest request,HttpServletResponse response){
		model.addAttribute("user",user);
		//取缓存
		String html = redisUtil.get(GoodsKey.getGoodsList,"",String.class);
		if (!StringUtils.isEmpty(html)){
			return html;
		}

		//查询商品列表
		List<GoodsVo> goodsList = goodsService.listGoodsVo();
		model.addAttribute("goodsList",goodsList);
//		return "goods_list";

		IWebContext context = new WebContext(request,response,
				  							request.getServletContext(),
											request.getLocale(),
											model.asMap()
											);
		//手动渲染
		html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context);

		if (!StringUtils.isEmpty(html)) {
			redisUtil.set(GoodsKey.getGoodsList, "", html);
		}
		return html;
	}


//	@RequestMapping(value = "/to_detail2/{goodsId}",produces = "text/html")
//	@ResponseBody
//	public String detail(Model model,MiaoshaUser user,
//						 @PathVariable("goodsId")long goodsId,
//						 HttpServletRequest request,
//						 HttpServletResponse response
//						 ){
//		model.addAttribute("user",user);
//
//		//取缓存
//		String html = redisUtil.get(GoodsKey.getGoodsDetail,""+goodsId,String.class);
//		if (!StringUtils.isEmpty(html)){
//			return html;
//		}
//
//		GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
//		model.addAttribute("goods",goods);
//
//		long startAt = goods.getStartDate().getTime();
//		long endAt = goods.getEndDate().getTime();
//		long now = System.currentTimeMillis();
//
//		int miaoshaStatus = 0;//秒杀状态码
//		int remainSeconds = 0;//剩余时间
//		if (now < startAt){//秒杀尚未开始
//			miaoshaStatus = 0;
//			remainSeconds = (int)(startAt - now)/1000;
//		}else if(now > endAt) {//秒杀已经结束
//			miaoshaStatus = 2;
//			remainSeconds = -1;
//		}else{//秒杀正在进行
//			miaoshaStatus = 1;
//			remainSeconds = 0;
//		}
//		model.addAttribute("miaoshaStatus",miaoshaStatus);
//		model.addAttribute("remainSeconds",remainSeconds);
//
////		return "goods_detail";
//		IWebContext context = new WebContext(request,response,
//				request.getServletContext(),
//				request.getLocale(),
//				model.asMap()
//		);
//		//手动渲染
//		html = thymeleafViewResolver.getTemplateEngine().process("goods_detail",context);
//
//		if (!StringUtils.isEmpty(html)) {
//			redisUtil.set(GoodsKey.getGoodsDetail, ""+goodsId, html);
//		}
//		return html;
//
//	}

	@RequestMapping(value="/detail/{goodsId}")
	@ResponseBody
	public Result<GoodsDetailVo> detail(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser user,
										@PathVariable("goodsId")long goodsId) {
		GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
		long startAt = goods.getStartDate().getTime();
		long endAt = goods.getEndDate().getTime();
		long now = System.currentTimeMillis();
		int miaoshaStatus = 0;
		int remainSeconds = 0;
		if(now < startAt ) {//秒杀还没开始，倒计时
			miaoshaStatus = 0;
			remainSeconds = (int)((startAt - now )/1000);
		}else  if(now > endAt){//秒杀已经结束
			miaoshaStatus = 2;
			remainSeconds = -1;
		}else {//秒杀进行中
			miaoshaStatus = 1;
			remainSeconds = 0;
		}
		GoodsDetailVo vo = new GoodsDetailVo();
		vo.setGoods(goods);
		vo.setUser(user);
		vo.setRemainSeconds(remainSeconds);
		vo.setMiaoshaStatus(miaoshaStatus);
		return Result.success(vo);
	}


    
}
